package org.example.com.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.core.injector.methods.DeleteById;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.SneakyThrows;
import org.example.com.Bean.User;
import org.example.com.config.JwtConfig;
import org.example.com.exception.*;
import org.example.com.mapper.UserMapper;
import org.example.com.service.UserService;
import org.example.com.util.JsonResult;
import org.example.com.util.RecultTool;
import org.example.com.util.StringUtil;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.data.redis.core.ValueOperations;
import org.springframework.stereotype.Service;
import java.util.List;
@Service
public class UserServiceImpl extends ServiceImpl<UserMapper, User> implements UserService {
    private final StringRedisTemplate stringRedisTemplate;
    public UserServiceImpl(StringRedisTemplate stringRedisTemplate) {
        this.stringRedisTemplate = stringRedisTemplate;
    }
    @Override
   public JsonResult findUser(){
       return RecultTool.success(list());
   }
   @Override
   public JsonResult findUser(int page){
        Page<User> p= Page.of(page, 10);
        List<User> list=list(p);
        p.setRecords(list);
        return RecultTool.success(p);
    }
    @Override
    public JsonResult findById(String id) {
        User byId = getById(id);
        return RecultTool.success(byId);
    }
    @Override
    public JsonResult findByUserName(String userName,int page) {
        QueryWrapper<User> wrapper = new QueryWrapper<>();
        //设置条件
        wrapper.like("username", userName);
        Page<User> p1= Page.of(page, 10);
        List<User> list = list(p1, wrapper);
        //查询
        p1.setRecords(list);
        return RecultTool.success(p1);
    }
    @Override
    public JsonResult saveUser(User user) {
        boolean save = save(user);
        if (!save) {
            throw new userSaveException("添加用户失败");
        }
        return RecultTool.success("success");
    }

    @Override
    public JsonResult updateUser(User user) {
        boolean b = updateById(user);
        if (!b) {
            throw new userSaveException("修改用户失败");
        }
        return RecultTool.success("success");

    }

    @SneakyThrows
    @Override
    public JsonResult updateUserPassword(String id, String newPass) {
        UpdateWrapper<User> Wrapper = new UpdateWrapper<>();
        Wrapper.eq("id", id);
        Wrapper.set("password", StringUtil.encryptMD5(newPass));
        boolean b = update(Wrapper);
        if (!b) {
            throw new userUpdateException("修改用户密码失败");
        }
        return RecultTool.success("success");
    }

    @Override
    public JsonResult deleteUser(String id) {
        boolean b = removeById(id);
        if (!b) {
            throw new userRemoveException("修改用户失败");
        }
        return RecultTool.success("success");
    }

    @Override
    public JsonResult updateUserStatus(String id, int status) {
        UpdateWrapper<User> wrapper = new UpdateWrapper<>();
        wrapper.eq("id", id);
        wrapper.set("status", status);
        boolean b = update(wrapper);
        if (!b) {
          throw new userRemoveException("修改状态失败");
        }
        return RecultTool.success("success");
    }
    @SneakyThrows
    @Override
    public JsonResult checkPasswordExists(String id, String password) {
        UpdateWrapper<User> wrapper = new UpdateWrapper<>();
        wrapper.eq("id", id);
        wrapper.eq("password", StringUtil.encryptMD5(password));
        User one = getOne(wrapper);
        if (one == null) {
            throw  new passwordNotExicts("你的原始密码错误");
        }
       return RecultTool.success("success");

    }
    public JsonResult login(User user) {
       QueryWrapper<User> wrapper = new QueryWrapper<>();
       wrapper.eq("username", user.getUsername());
       wrapper.eq("password", user.getPassword());
       User one = getOne(wrapper);
       if (one == null) {
           throw new UsernamePassowrdException("账号或者密码错误");
       }
        String token = JwtConfig.getJwtToken(one);
        ValueOperations<String, String> ops = stringRedisTemplate.opsForValue();
        ops.set("token:"+one.getId(),token);
        return RecultTool.success(token);

    }
}